/*
 * 
 */
package interfaces;
	
import java.awt.EventQueue;
	
	import javax.swing.JFrame;
	import javax.swing.JPanel;
	import javax.swing.JScrollPane;
	import javax.swing.JLabel;
	import javax.swing.JTextField;
	import javax.swing.JSeparator;
	import javax.swing.SwingConstants;
	import java.awt.SystemColor;
	import javax.swing.JTextArea;
	
	import abstractions.*;

	import javax.swing.JButton;

	import java.awt.event.ActionListener;
	import java.awt.event.ActionEvent;
	
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.LinkedList;
	
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPasswordField;

/**
 * @author rdru
 * INV: 
 */
@SuppressWarnings("rawtypes")
public class ClientStub extends JFrame {
	//private static final long serialVersionUID = 1L;
	public JFrame monFrame;
	private JPanel contentPane;
	private JTextField txtdocId;
	private JTextField txtAtomId;
	private JTextField txtUserId;
	private static JTextArea clientlog;
	
	static StringBuilder fullLog = new StringBuilder();
	private JTextField txtlogname;
	private JTextField txtDocName;
	private JTextField txtdocPath;
	
  	/*
	 **Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientStub frame = new ClientStub();
					frame.setVisible(true);    
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}


	private ServerServices server;
	private ClientServicesStub client;
	private String ip;
	private JPasswordField txtpassword;
	private JTextField txtClientId;
	OpenDocInterface od;
	

	public ClientStub() throws RemoteException {
		
		final JTextArea txtdoctext = new JTextArea();
		monFrame = this;
		setTitle("Client Stub");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(5, 5, 643, 766);
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblOpenDocuments = new JLabel("docId");
		lblOpenDocuments.setBounds(19, 48, 115, 16);
		contentPane.add(lblOpenDocuments);
		
		txtdocId = new JTextField();
		txtdocId.setHorizontalAlignment(SwingConstants.RIGHT);
		txtdocId.setText("1");
		txtdocId.setBounds(142, 42, 59, 21);
		contentPane.add(txtdocId);
		txtdocId.setColumns(10);
		
		JLabel lblRegisteredUsers = new JLabel("AtomId");
		lblRegisteredUsers.setBounds(19, 81, 122, 16);
		contentPane.add(lblRegisteredUsers);
		
		txtAtomId = new JTextField();
		txtAtomId.setHorizontalAlignment(SwingConstants.RIGHT);
		txtAtomId.setText("1");
		txtAtomId.setBounds(142, 75, 59, 21);
		contentPane.add(txtAtomId);
		txtAtomId.setColumns(10);
		
		JLabel lblActiveUsers = new JLabel("UserId");
		lblActiveUsers.setBounds(19, 114, 109, 16);
		contentPane.add(lblActiveUsers);
		
		txtUserId = new JTextField();
		txtUserId.setHorizontalAlignment(SwingConstants.RIGHT);
		txtUserId.setBounds(142, 108, 59, 21);
		contentPane.add(txtUserId);
		txtUserId.setColumns(10);
		
		JLabel lblServerStatusLog = new JLabel("Client Status Log");
		lblServerStatusLog.setBounds(10, 425, 128, 16);
		contentPane.add(lblServerStatusLog);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(10, 450, 598, 267);
		contentPane.add(scrollPane_2);
		
		clientlog = new JTextArea();
		clientlog.setEnabled(false);
		clientlog.setLineWrap(true);
		scrollPane_2.setViewportView(clientlog);
		
		JSeparator separator = new JSeparator();
		separator.setForeground(SystemColor.windowBorder);
		separator.setOrientation(SwingConstants.VERTICAL);
		separator.setBounds(347, 17, 12, 376);
		contentPane.add(separator);
		
		JSeparator separator_2 = new JSeparator();
		separator_2.setForeground(SystemColor.windowBorder);
		separator_2.setBounds(10, 404, 607, 10);
		contentPane.add(separator_2);
		
		JButton btnNewButton = new JButton("login");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				clientlog.append("login ("+ client.toString() + " , " + txtClientId.getText() + " , " + txtlogname.getText() + "," + new String (txtpassword.getPassword()) + ")\n");			
				try {
					txtUserId.setText(server.login(client, Integer.parseInt(txtClientId.getText()) , txtlogname.getText(), new String (txtpassword.getPassword()))+"");
					if ( txtUserId.getText().equals("-1"))
					{
						clientlog.append("------Login errado.\n");
					}
					else
						clientlog.append("------Login executado com sucesso.\n");
						
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				}
		});
		btnNewButton.setBounds(432, 48, 116, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("logout");
		btnNewButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				clientlog.append("logout ("+ txtClientId.getText() + " , " + txtUserId.getText() + ")\n");			
				try {
					if (server.logout(Integer.parseInt(txtClientId.getText()), Integer.parseInt(txtUserId.getText())) == false)
					{
						clientlog.append("------Logout errado.\n");
					}
					else
						clientlog.append("------Logout executado com sucesso.\n");
						
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		btnNewButton_1.setBounds(432, 85, 116, 23);
		contentPane.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("addDocument");
		btnNewButton_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				clientlog.append("addDocument ("+ txtClientId.getText() + " , " + txtUserId.getText() + "," + txtDocName.getText() + "," +  txtdoctext.getText() +")\n");			
				try {
					if (server.docAdd(Integer.parseInt(txtClientId.getText()), Integer.parseInt(txtUserId.getText()), txtDocName.getText(), txtdoctext.getText() )  == false)
					{
						clientlog.append("addDocument errado.\n");
					}
					else
						clientlog.append("addDocument executado com sucesso.\n");
						
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		btnNewButton_2.setBounds(432, 119, 116, 23);
		contentPane.add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("removeDocument");
		btnNewButton_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				clientlog.append("removeDocument ("+ txtClientId.getText() + " , " + txtUserId.getText() + "," + txtDocName.getText()+")\n");			
				try {
					if (server.docRemove(Integer.parseInt(txtClientId.getText()), Integer.parseInt(txtUserId.getText()), txtDocName.getText())  == false)
					{
						clientlog.append("removeDocument errado.\n");
					}
					else
						clientlog.append("removeDocument executado com sucesso.\n");
						
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			}
		});
		btnNewButton_3.setBounds(432, 153, 116, 23);
		contentPane.add(btnNewButton_3);
		
		JButton btnNewButton_4 = new JButton("docOpen");
		btnNewButton_4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				clientlog.append("docOpen ("+ txtClientId.getText() + " , " + txtUserId.getText() + "," + txtdocPath.getText() + ")\n");			
				try {
					od = (OpenDocInterface) server.docOpen(Integer.parseInt(txtClientId.getText()), Integer.parseInt(txtUserId.getText()), txtdocPath.getText());
					if (od == null)
					{
						clientlog.append("docOpen errado.\n");
					}
					else
						clientlog.append("docOpen executado com sucesso.\n");
						
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}						
			}
		});
		btnNewButton_4.setBounds(432, 187, 116, 23);
		contentPane.add(btnNewButton_4);
		
		JButton btnNewButton_5 = new JButton("docClose");
		btnNewButton_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				clientlog.append("docClose ("+ txtClientId.getText() + " , " + txtUserId.getText() + ","+ txtdocId.getText() + ")\n");			
				try {
					
						server.docClose(Integer.parseInt(txtClientId.getText()), Integer.parseInt(txtUserId.getText()), Integer.parseInt(txtdocId.getText()));
						clientlog.append("docClose executado com sucesso.\n");
						
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}						
			}
		});
		btnNewButton_5.setBounds(432, 258, 116, 23);
		contentPane.add(btnNewButton_5);
		
		JButton btnNewButton_6 = new JButton("lockRequest");
		btnNewButton_6.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				AtomId aux = new AtomId(new ArrayList<Integer>(Integer.parseInt(txtAtomId.getText())));
				clientlog.append("lockRequest ("+ txtClientId.getText() + " , " + txtUserId.getText() + ","+ txtdocId.getText()+ "," + aux.toString()+")\n");			
				try {
					
					if ( server.lockRequest(Integer.parseInt(txtClientId.getText()), Integer.parseInt(txtUserId.getText()),Integer.parseInt(txtdocId.getText()),aux) == false)
					{
						clientlog.append("lockRequest errado.\n");
					}
					else
						clientlog.append("lockRequest executado com sucesso.\n");
						
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}									
			}
		});
		btnNewButton_6.setBounds(432, 292, 116, 23);
		contentPane.add(btnNewButton_6);
		
		JButton btnNewButton_7 = new JButton("lockRelease");
		btnNewButton_7.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				AtomId aux = new AtomId(new ArrayList<Integer>(Integer.parseInt(txtAtomId.getText())));
				clientlog.append("lockRelease ("+ txtClientId.getText() + " , " + txtUserId.getText() + ","+ txtdocId.getText()+ "," + aux.toString()+")\n");			
				try {
					
					if ( server.lockRelease(Integer.parseInt(txtClientId.getText()), Integer.parseInt(txtUserId.getText()),Integer.parseInt(txtdocId.getText()), aux) == false)
					{
						clientlog.append("lockRelease  errado.\n");
					}
					else
						clientlog.append("lockRelease  executado com sucesso.\n");
						
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}									
			}
		});
		btnNewButton_7.setBounds(432, 324, 116, 23);
		contentPane.add(btnNewButton_7);
		
		JButton btnNewButton_8 = new JButton("updateDoc");
		btnNewButton_8.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				LinkedList<CommandC> lista1 = new LinkedList<CommandC>();
				lista1.add(new CommandC(1 , 1 , new PosC(new AtomId(new ArrayList<Integer>(1)),0) , new Part("") , new Part("")));
				lista1.add(new CommandC(1 , 1 , new PosC(new AtomId(new ArrayList<Integer>(2)),0) , new Part("") , new Part("")));
				lista1.add(new CommandC(1 , 1 , new PosC(new AtomId(new ArrayList<Integer>(3)),0) , new Part("") , new Part("")));
				// trocando os dois primeiros versos de lugar
				lista1.add(new CommandC(1 , 1 , new PosC(new AtomId(new ArrayList<Integer>(1)),0) , new Part("Minhas duvidas sao incertas") , new Part("Minhas certezas duvidosas")));
				lista1.add(new CommandC(1 , 1 , new PosC(new AtomId(new ArrayList<Integer>(1)),0) , new Part("Minhas certezas duvidosas") , new Part("Minhas duvidas sao incertas")));
				
				clientlog.append("updateOpen ("+ txtClientId.getText() + " , " + txtUserId.getText() + "," + txtdocId.getText()+")\n");			
				try {
					
					if ( server.updateDoc(Integer.parseInt(txtClientId.getText()),Integer.parseInt(txtUserId.getText()), Integer.parseInt(txtdocId.getText()),lista1) == false)
					{
						clientlog.append("updateDoc errado.\n");
					}
					else
						clientlog.append("updateDoc executado com sucesso.\n");
						
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}						
			}
		});
		btnNewButton_8.setBounds(432, 224, 115, 23);
		contentPane.add(btnNewButton_8);
		
		JLabel lblLogname = new JLabel("logname");
		lblLogname.setBounds(19, 150, 109, 16);
		contentPane.add(lblLogname);
		
		txtlogname = new JTextField();
		txtlogname.setHorizontalAlignment(SwingConstants.RIGHT);
		txtlogname.setText("rog");
		txtlogname.setColumns(10);
		txtlogname.setBounds(142, 140, 134, 21);
		contentPane.add(txtlogname);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(19, 184, 109, 16);
		contentPane.add(lblPassword);
		
		JLabel lblDocname = new JLabel("docName");
		lblDocname.setBounds(19, 219, 109, 16);
		contentPane.add(lblDocname);
		
		txtDocName = new JTextField();
		txtDocName.setHorizontalAlignment(SwingConstants.RIGHT);
		txtDocName.setText("initDocInput.txt");
		txtDocName.setColumns(10);
		txtDocName.setBounds(142, 217, 134, 21);
		contentPane.add(txtDocName);
		
		txtdocPath = new JTextField();
		txtdocPath.setHorizontalAlignment(SwingConstants.RIGHT);
		txtdocPath.setText("initDocInput.txt");
		txtdocPath.setColumns(10);
		txtdocPath.setBounds(142, 251, 134, 21);
		contentPane.add(txtdocPath);
		
		JLabel lblDocpath = new JLabel("docPath");
		lblDocpath.setBounds(19, 253, 109, 16);
		contentPane.add(lblDocpath);
		
		client = new ClientServicesStub(clientlog);
		
		JLabel lblDoc = new JLabel("docText");
		lblDoc.setBounds(19, 290, 109, 16);
		contentPane.add(lblDoc);
		
		txtpassword = new JPasswordField();
		txtpassword.setHorizontalAlignment(SwingConstants.RIGHT);
		txtpassword.setColumns(10);
		txtpassword.setBounds(142, 182, 134, 21);
		contentPane.add(txtpassword);
		
		txtClientId = new JTextField();
		txtClientId.setHorizontalAlignment(SwingConstants.RIGHT);
		txtClientId.setText("1");
		txtClientId.setColumns(10);
		txtClientId.setBounds(142, 11, 59, 21);
		contentPane.add(txtClientId);
		
		JLabel lblClientid = new JLabel("clientId");
		lblClientid.setBounds(19, 17, 115, 16);
		contentPane.add(lblClientid);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(142, 285, 156, 108);
		contentPane.add(scrollPane);
		
		
		scrollPane.setViewportView(txtdoctext);
		txtdoctext.setText("Minhas duvidas sao incertas\nMinhas certezas duvidosas\nMinhas contradicoes se confirmam\nMeu lirismo e unissono\nMinha vontade ociosa\nMeu romantismo carnal\nMinha filosofia e plana\nMinha bondade tem gana\nMinha calma e letal\n");
		//make rmi		
		rmiClient aux  = new rmiClient();
		server = aux.rmiClientHandler(ServerServices.class, "localhost");
		
		try {
			ip = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
